package com.fancy.learncenter.utils;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;

import com.fancy.learncenter.ui.activity.GuideActivity;
import com.fancy.learncenter.ui.activity.MainActivity;
import com.fancy.learncenter.ui.activity.SplashActivity;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;


/**
 * Created by hyy on 2017/10/19.
 * describe as  计时器工具类
 */

public class TimeUtil {

//    public static Disposable disposables;

    /**
     * 轮询器
     *
     * @param milseconds
     */
    public static void interval(final long milseconds, final TimeCallBack timeCallBack) {
        mObservable = Observable.interval(milseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread());
        subscription = mObservable.subscribe(new Observer<Long>() {
            @Override
            public void onCompleted() {
                timeCallBack.completeCallBack();
            }

            @Override
            public void onError(Throwable e) {
                timeCallBack.completeCallBack();
            }

            @Override
            public void onNext(Long aLong) {
                timeCallBack.nextCallBack(aLong);
            }
        });
    }

    static Observable observable;

    static Observer observer;
    private static Observable<Long> mObservable;

    static Subscription subscription;

    /**
     * 倒计时
     *
     * @param mseconds
     */
    public static void timer(long mseconds, final TimeCallBack timeCallBack) {
        mObservable = Observable.timer(mseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread());
        subscription = mObservable.subscribe(new Observer<Long>() {
            @Override
            public void onCompleted() {
                timeCallBack.completeCallBack();
                cancle();
            }

            @Override
            public void onError(Throwable e) {
                timeCallBack.completeCallBack();
                cancle();
            }

            @Override
            public void onNext(Long aLong) {
                timeCallBack.nextCallBack(aLong);
            }
        });
    }

    public interface TimeCallBack {
        /**
         * 计时中
         */
        abstract void nextCallBack(long time);

        /**
         * 计时完成
         */
        abstract void completeCallBack();

        /**
         * 计时开始
         */
        abstract void startCallBack();
    }

    public static void cancle() {

        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }

    }


    /**
     * 返回 11'22"样式
     *
     * @param
     * @return
     */
    public static String localDubTimeLength(int mseconds) {
        int seconds = mseconds / 1000;
        String str = "";
        if (seconds < 60) {
            str = seconds + "\"";
        } else {
            int minutes = seconds / 60;
            int endSeconds = seconds % 60;
            str = str + minutes + "'" + endSeconds + "\"";
        }

        return str;
    }

    /**
     * 获取时分秒格式的日期字符串
     *
     * @param millseconds
     * @return
     */
    public static String timeAllFormate(long millseconds) {
        String str = "";
        int seconds = (int) (millseconds / 1000);
        if (seconds < 60) {
            str = "00:00:" + seconds;
        } else if (seconds < 3600) {
            int minutes = seconds / 60;
            int endSeconds = seconds % 60;
            str = "00:" + minutes + ":" + endSeconds;
        } else {
            int hours = seconds / 3600;
            int endSeconds = seconds % 3600;
            int minutes = endSeconds / 60;
            int lastSeconds = endSeconds % 60;
            str = hours + ":" + minutes + ":" + lastSeconds;
        }
        Log.i("str----", str);
        return str;

    }
}
